home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / A-B / ASCII.cpt / ASCII.C next >
C/C++ Source or Header  |  1985-10-19  |  12KB  |  497 lines

  1. #include <mac/quickdraw.h>
  2. #include <mac/toolintf.h>
  3. #include <mac/osintf.h>
  4. #include <mac/packintf.h>
  5. #include "ascii.h"
  6.  
  7. main () {
  8.     /* Standard initialisation */
  9.     struct QDVar QDVar;
  10.  
  11.     QD = &QDVar;
  12.     InitGraf (&thePort);
  13.     InitFonts ();
  14.     FlushEvents (everyEvent, 0);
  15.     InitWindows ();
  16.     InitMenus ();
  17.     TEInit ();
  18.     InitDialogs ( (ProcPtr) 0);
  19. /*  InitCursor ();                       Done in the LOOP  */
  20.  
  21.     /* Application dependent initialisation */
  22.     init_the_w ();
  23.     init_the_text ();
  24.     init_the_menu ();
  25.     init_the_scrap ();
  26.     init_the_cursor ();
  27.     init_the_resource ();        /* This must be the last one ... */
  28.  
  29. /* The Main Event LOOP */
  30.     while (1){
  31.     TEIdle (h_text);
  32.     SystemTask ();
  33.     GetNextEvent (everyEvent, &my_event);
  34.     if (the_w == FrontWindow ()){
  35.         adjust_cursor ();
  36.         mouse_p.h = my_event.where.h;
  37.         mouse_p.v = my_event.where.v;
  38.         GlobalToLocal (&mouse_p);
  39.     }
  40.     switch (my_event.what){
  41.         case mouseDown:
  42.         switch (FindWindow (&my_event.where, &which_w)){
  43.             case inMenuBar:
  44.             do_command (MenuSelect (&my_event.where));
  45.             break;
  46.             case inContent:
  47.             if (the_w == FrontWindow ())
  48.                 the_w_click ();
  49.             else
  50.                 SelectWindow (which_w);
  51.             break;
  52.             case inDrag:
  53.             break;    /* No drag done */
  54.             case inGoAway:
  55.             break;    /* Ne exit availible */
  56.             case inSysWindow:
  57.             SystemClick (&my_event, which_w);
  58.             break;
  59.         }
  60.         break;
  61.         case keyDown:
  62.         if (the_w != FrontWindow ())
  63.             break;
  64.         do_key ();
  65.         break;
  66.         case activateEvt:
  67.         if (the_w == FrontWindow ()){
  68.             TEActivate (h_text);
  69.             DisableItem (my_menus[2], TEUNDO);
  70.             EnableItem (my_menus[3], 0);
  71.             EnableItem (my_menus[4], 0);
  72.         }
  73.         else{
  74.             TEDeactivate (h_text);
  75.             EnableItem (my_menus[2], TEUNDO);
  76.             DisableItem (my_menus[3], 0);
  77.             DisableItem (my_menus[4], 0);
  78.         }
  79.         DrawMenuBar ();
  80.         break;
  81.         case updateEvt:
  82.         BeginUpdate (the_w);
  83.         SetCursor (*watch_c);
  84.         draw_the_w ();
  85.         TEUpdate (&text_r, h_text);
  86.         ShowCursor ();
  87.         EndUpdate (the_w);
  88.         break;
  89.      }
  90.     }
  91. }
  92.  
  93. init_the_w (){
  94.     int x, y;
  95.  
  96.     the_w = GetNewWindow (WINDOW, &w_record,  (WindowPtr) - 1);
  97.     SetRect (&ascii_r, 18, 18, 418, 320);
  98.     SetRect (&text_r, 425, 18, 500, 317);
  99.     SetRect (&view_r, 420, 0, 512, 322);
  100.     for (x = 0; x < 16; x++)
  101.     for (y = 0; y < 16; y++)
  102.         SetRect (&char_r[x][y], 18+25*x, 18+19*y, 43+25*x, 37+19*y);
  103.     SetPort (the_w);
  104. }
  105.  
  106. init_the_text (){
  107.     char temp_str[256];
  108.  
  109.     h_text = TENew (&text_r, &view_r);               /* view_r > text_r */
  110.     h_authortext = TENew (&text_r, &view_r);
  111.     sample_str = GetString(SAMPLESTR);
  112.     author_str = GetString(AUTHORSTR);
  113.     c2p_string(*sample_str, temp_str);
  114.     TESetText (temp_str, (int) (*sample_str)[0], h_text);
  115.     c2p_string(*author_str, temp_str);
  116.     TESetText (temp_str, (int) (*author_str)[0], h_authortext);
  117. }
  118.  
  119. init_the_resource (){
  120.     file_num = root_system = HomeResFile ( GetIndResource ("FONT", 1) );
  121.     my_own_file = CurResFile ();
  122. }
  123.  
  124. change_font (size) int size;{   /* 0 means new font => chose smallest */
  125.     int font, i;
  126.  
  127.     for (i = 1; i <= num_of_sizes; i++)
  128.     CheckItem (my_menus[4], i, 0);
  129.     GetItem (my_menus[3], which_font, font_name);
  130.     GetFNum (font_name, &font);
  131.     if (! size){
  132.     for (i = 1; i <= num_of_sizes; i++)
  133.         if (RealFont (font, font_sizes[i])){
  134.         size = font_sizes[i];
  135.         break;
  136.         }
  137.     if (size > 18)
  138.         size = 12;
  139.     }
  140.     if (! size)
  141.     size = 12;
  142.     (*h_text)->fontAscent =  (size * 3) / 4;          /* How do this right ? */
  143.     (*h_text)->lineHeight =  (*h_text)->txSize = size;
  144.     (*h_text)->txFont = font;
  145.     TECalText (h_text);
  146.     for (i = 1; i <= num_of_sizes; i++)
  147.     if (the_w->txSize == font_sizes[i])
  148.         CheckItem (my_menus[4], i, 1);
  149. }
  150.  
  151. init_the_menu (){
  152.     int i, dummy;
  153.  
  154.     my_menus[0] = GetMenu(APPLE);
  155.     (**my_menus[0]).menuData.s[1] = '\024'; /* Silly thing this !! */
  156.     AddResMenu (my_menus[0], "DRVR");
  157.     InsertMenu (my_menus[0], 0);
  158.  
  159.     my_menus[1] = GetMenu (FILE);
  160.     InsertMenu (my_menus[1], 0);
  161.  
  162.     my_menus[2] = GetMenu (EDIT);
  163.     InsertMenu (my_menus[2], 0);
  164.  
  165.     add_font_menu ();
  166.  
  167.     DrawMenuBar ();
  168. }
  169.  
  170. add_font_menu (){
  171.     my_menus[3] = NewMenu (FONTS, "Font");
  172.     AddResMenu (my_menus[3], "FONT");
  173.     InsertMenu (my_menus[3], 0);
  174.     which_font = 1;
  175.     CheckItem (my_menus[3], which_font, 1);
  176.     change_font (12);
  177.     add_size_menu ();
  178.     change_font (0);
  179. }
  180.  
  181. add_size_menu (){
  182.     char name[64];
  183.     int i;
  184.  
  185.     my_menus[4] = NewMenu (FONTSIZE, font_name);
  186.  
  187.     num_of_sizes = 0;
  188.     for (i = 1; i < 128; i++)
  189.     if (RealFont (the_w->txFont, i)){
  190.         NumToString (i, name);
  191.         append_string ("<O", name);
  192.         AppendMenu (my_menus[4], name);
  193.         font_sizes[++num_of_sizes] = i;
  194.     }
  195.     else
  196.         if (i == 9 || i == 10 || i == 12 || i == 14 || i == 18){
  197.         NumToString (i, name);
  198.         AppendMenu (my_menus[4], name);
  199.         font_sizes[++num_of_sizes] = i;
  200.         }
  201.     InsertMenu (my_menus[4], 0);
  202.     for (i = 1; i <= num_of_sizes; i++)
  203.     if (the_w->txSize == font_sizes[i])
  204.         CheckItem (my_menus[4], i, 1);
  205. }
  206.  
  207. append_string (app, str) char *app, *str;{
  208.     int i, j;
  209.  
  210.     i = 0;
  211.     while (str[i++] != 0 && i < 60);
  212.     i--;
  213.  
  214.     j = 0;
  215.     while (app[j] != 0 && i + j < 60)
  216.     str[i++] = app[j++];
  217.     str[i] = 0;
  218. }
  219.  
  220. init_the_cursor (){
  221.     finger_c = GetCursor (FINGER); HNoPurge (finger_c);
  222.     watch_c = GetCursor (WATCH); HNoPurge (watch_c);
  223.     bar_c = GetCursor (BAR); HNoPurge (bar_c);
  224. }
  225.  
  226. init_the_scrap (){
  227.     ScrapStuff *scrap_stuff;
  228.  
  229.     scrap_stuff = InfoScrap ();
  230.     scrap_count = ! ( scrap_stuff->scrapCount );
  231. /*  get_scrap ();                Dosn't work yet   */
  232. }
  233.  
  234. do_key (){
  235.     char the_char;
  236.  
  237.     the_char = my_event.message & 0xff;
  238.     InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
  239.     TEKey (the_char, h_text);
  240.     InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
  241. }
  242.  
  243. draw_the_w (){
  244.     int x, y, save_font, save_size;
  245.  
  246.     EraseRect (&the_w->portRect);
  247.     FrameRect (&ascii_r);
  248.  
  249.     for (x = 0; x < 16; x++){
  250.     save_font = the_w->txFont;
  251.     save_size = the_w->txSize;
  252.     the_w->txFont = systemFont;
  253.     the_w->txSize = 12;
  254.     MoveTo (5, char_r[0][x].top + 14);
  255.     if (x < 10)
  256.         DrawChar ( (char) ( x + '0') );
  257.     else
  258.         DrawChar ( (char) ( x + 'a' - 10) );
  259.     MoveTo (char_r[x][0].topLeft.h + 5, 10);
  260.     if (x < 10)
  261.         DrawChar ( (char) ( x + '0') );
  262.     else
  263.         DrawChar ( (char) ( x + 'a' - 10) );
  264.     the_w->txFont = save_font;
  265.     the_w->txSize = save_size;
  266.     for (y = 0; y < 16; y++){
  267.         MoveTo (char_r[x][y].topLeft.h + 5, char_r[x][y].top + 14);
  268.         DrawChar ( (char) ( x*16 + y ) );
  269.     }
  270.     }
  271. }
  272.  
  273. do_command (help) int help;{
  274.     char name[64];
  275.     int i;
  276.  
  277.     the_menu =  HiWord (help);
  278.     the_item =  LoWord (help);
  279.     switch (the_menu){
  280.     case APPLE:
  281.         if(the_item == ABOUT){
  282.         about();
  283.         break;
  284.         }
  285.         if(the_item == ABOUT + 1)
  286.         break;
  287.         GetItem (my_menus[0], the_item, name);
  288.         {
  289.         GrafPtr save_port;
  290.         char *da_name ();
  291.  
  292.         GetPort (&save_port);
  293.         if (GetNamedResource ("DRVR", name))/* Without leading zero */
  294.             OpenDeskAcc (name);
  295.         else                                /* With leading zero    */
  296.             OpenDeskAcc (da_name (name));
  297.         SetPort (save_port);
  298.         }
  299.         break;
  300.     case FILE:
  301.         switch (the_item){
  302.         case OPEN:
  303.             open_file ();
  304.             break;
  305.         case QUIT:
  306.             LoadScrap ();
  307.             ExitToShell ();
  308.             break;
  309.         }
  310.         break;
  311.     case EDIT:
  312.         if (SystemEdit (the_item - 1))
  313.         break;
  314.         switch (the_item){
  315.         case TECUT:
  316.             TECut (h_text);
  317.             put_scrap ();
  318.             break;
  319.         case TECOPY:
  320.             TECopy (h_text);
  321.             put_scrap ();
  322.             break;
  323.         case TEPASTE:
  324. /*                  get_scrap ();        Dosn't work yet   */
  325.             TEPaste (h_text);
  326.             break;
  327.         case TECLEAR:
  328.             TEDelete (h_text);
  329.             break;
  330.         }
  331.         break;
  332.     case FONTS:
  333.         CheckItem (my_menus[3], which_font, 0);
  334.         which_font = the_item;
  335.         change_font (0);
  336.         CheckItem (my_menus[3], which_font, 1);
  337.  
  338.         num_of_sizes = 0;
  339.         DeleteMenu (FONTSIZE);
  340.         add_size_menu ();
  341.         DrawMenuBar ();
  342.  
  343.         InvalRect (&the_w->portRect);
  344.         break;
  345.     case FONTSIZE:
  346.         change_font (font_sizes[the_item]);
  347.         InvalRect (&the_w->portRect);
  348.     break;
  349.     }
  350.     HiliteMenu (0);
  351. }
  352.  
  353. adjust_cursor (){
  354.     if (PtInRect (&mouse_p, &ascii_r)){
  355.     SetCursor (*finger_c);
  356.     return;
  357.     }
  358.     if (PtInRect (&mouse_p, &text_r)){
  359.     SetCursor (*bar_c);
  360.     return;
  361.     }
  362.     InitCursor ();
  363. }
  364.  
  365. about(){
  366.     EraseRect (&text_r);
  367.     TEUpdate (&text_r, h_authortext);
  368.     while (! Button ());
  369.     while (Button ());
  370.     EraseRect (&text_r);
  371.     TEUpdate (&text_r, h_text);
  372.     FlushEvents (everyEvent, 0);
  373. }
  374.  
  375. the_w_click (){
  376.     int x, y;
  377.  
  378.     if (PtInRect (&mouse_p, &ascii_r)){
  379.     x =  (mouse_p.h - 18) / 25;
  380.     y =  (mouse_p.v - 18) / 19;
  381.     InvertRect (&char_r[x][y]);
  382.     TEKey ( (char) ( x*16 + y ) , h_text);
  383.     InvertRect (&char_r[x][y]);
  384.     }
  385.     if (PtInRect (&mouse_p, &text_r))
  386.     TEClick (&mouse_p, 0, h_text);
  387. }
  388.  
  389. put_scrap (){
  390.     Handle text_scrap;
  391.     ScrapStuff *scrap_stuff;
  392.  
  393.     ZeroScrap ();
  394.     text_scrap = (Ptr) TEScrpHandle;
  395.     text_scrap = (Ptr) *text_scrap;
  396.     HLock (text_scrap);
  397.     PutScrap (GetHandleSize (text_scrap), "TEXT", *text_scrap);
  398.     HUnlock (text_scrap);
  399.     scrap_stuff = InfoScrap ();
  400.     scrap_count = scrap_stuff->scrapCount;
  401. }
  402.  
  403. /* Dosn't work yet --------------------------------------------------------
  404. get_scrap (){
  405.     Handle hdl, text_scrap;
  406.     int text_length, off, *text_length_ptr;
  407.     ScrapStuff *scrap_stuff;
  408.  
  409.     scrap_stuff = InfoScrap ();
  410.     if (scrap_count == scrap_stuff->scrapCount)
  411.     return;
  412.     scrap_count = scrap_stuff->scrapCount;
  413.  
  414.     hdl = NewHandle (0);
  415.     text_length = GetScrap (hdl, "TEXT", &off);
  416.     if (text_length > 0){
  417.     text_scrap =  (Ptr) TEScrpHandle;
  418.     text_scrap =  (Ptr) *text_scrap;
  419.     SetHandleSize (text_scrap, text_length);
  420.     text_length_ptr =  (Ptr) TEScrpLength;
  421.     *text_length_ptr = text_length;
  422.     HLock (hdl);
  423.     HLock (text_scrap);
  424.     BlockMove (*hdl, *text_scrap, text_length);
  425.     HUnlock (hdl);
  426.     HUnlock (text_scrap);
  427.     }
  428.     DisposHandle (hdl);
  429. }
  430. ---------------------------------------------------------------------------*/
  431.  
  432. open_file (){
  433.     Point top_left;
  434.  
  435.     SetPt (&top_left, 50, 50);
  436.     SFGetFile (&top_left, "", (ProcPtr) 0, -1, "", (ProcPtr) 0, &file_record);
  437.     if (file_record.good){
  438.     if (file_num != root_system && file_num != my_own_file){
  439.         SetVol (0, volume_num);
  440.         CloseResFile (file_num);
  441.     }
  442.     volume_num = file_record.vRefNum;
  443.     SetVol (0, volume_num);
  444.     p2cstr (file_record.fName);
  445.     if (! EqualString ("System", file_record.fName, 0, 0) ){
  446.         file_num = OpenResFile (file_record.fName);
  447.         DeleteMenu (FONTS);
  448.         DeleteMenu (FONTSIZE);
  449.         add_font_menu ();
  450.         DrawMenuBar ();
  451.         InvalRect (&the_w->portRect);
  452.     }
  453.     else{
  454.         EraseRect (&ascii_r);
  455.         InvalRect (&ascii_r);
  456.         MoveTo (30, 30);
  457.         DrawString ("Can't open System file");
  458.         Delay (60);
  459.     }
  460.     }
  461. }
  462.  
  463. /*
  464.  * Some desk accessories have weird names that begin with a leading zero.
  465.  * Since this fools the automatic C/Pascal string conversion stuff, we
  466.  * have this little gem;  given a C string, it returns a Pascal string
  467.  * with leading zero.
  468.  */
  469. char *da_name (s)
  470.     char *s;
  471. {
  472.     static char ps[32];
  473.     int i = 1;
  474.     register char *cp,*dp;
  475.  
  476.     cp = s;
  477.     dp = &ps[2];
  478.     while ((*dp++ = *cp++))
  479.         i++;
  480.     ps[0] = i;
  481.     return (isapstr (ps));
  482. }
  483.  
  484. /*
  485.  * Copy a Pascal string into a buffer, converting it into a C string.
  486.  */
  487. c2p_string(pSource, cDestination)
  488. char *cDestination;
  489. char *pSource;
  490. {
  491.        int i;
  492.  
  493.        for (i = 0; i < pSource[0]; ++i)
  494.            cDestination[i] = pSource[i + 1];
  495.        cDestination[i] = '\0';
  496. }
  497.